package com.lh.arithmetic.sortarithmetic;

import com.lh.link.exception.EmptyException;

/**
 ************************************************
 ** Module : 单链表
 ** Date: 2012-8-1  Time: 上午10:32:26 
 ** Author lh 
 ************************************************ 
 */
public class SigalLinkList {
	private SigalLink first;
	
	public SigalLinkList(){
		first = null;
	}
	
	/**
	 * 功能描述  是否为空链表
	 * @Author: lh  2012-8-1 上午10:34:45
	 */
	public boolean isEmpty(){
		
//		if(null==first){
//			return true;
//		}else {
//			return false;
//		}
		//这个方法更直观
		return (null==first);
	}
	
	/**
	 * 功能描述  插入链表 
	 * @Author: lh  2012-8-1 上午10:37:20
	 */
	public void insertLink(SigalLink link){		
		link.next = first;
		first = link;
	}
	
	/**
	 * 功能描述  删除链表
	 * @throws EmptyException 
	 * @Author: lh  2012-8-1 上午10:37:42
	 */
	public SigalLink delLink() {
		if(!isEmpty()){
			SigalLink tempLink = first;
			first=first.next;
			return tempLink;
		}else {
			//throw new EmptyException();
			System.out.println("list is empty");
			return null;
		}	
		
	}
	
	/**
	 * 功能描述  显示List
	 * @Author: lh  2012-8-1 上午10:48:54
	 */
	public void displayList(){
		SigalLink currentLink = first;
		while (null!=currentLink) {			
			currentLink.displayLink();
			currentLink=currentLink.next;
		}		
	}
	
	/**
	 * 功能描述  查找
	 * @Author: lh  2012-8-1 上午11:07:52
	 */
	public SigalLink search(SigalLink keyLink) {
		if (!isEmpty()) {
			SigalLink currentLink = first;
			while (null != currentLink) {
				if (keyLink.intValue == currentLink.intValue) {
					System.out.println("find value=" + keyLink.intValue);
					return keyLink;
				} else {
					currentLink = currentLink.next;
				}				
			}
			// 找完了都没找到
			System.out.println("not find.");
			return null;
		} else {
			System.out.println("list is empty");
			return null;
		}	

	}
	
	/**
	 * 功能描述  删除指定的结点
	 * @Author: lh  2012-8-1 上午11:23:31
	 */
	public void del(SigalLink keyLink){
		if (!isEmpty()) {
			SigalLink currentLink = first;
			SigalLink previous =  null;
			while (null != currentLink) {
				if (keyLink.intValue == currentLink.intValue) {
					System.out.println("find value=" + keyLink.intValue);
					previous.next =  currentLink.next;
					System.out.println("del value " + keyLink.intValue+ " is ok ");
					return;
				} else {
					previous = currentLink;
					currentLink = currentLink.next;
				}				
			}
			// 找完了都没找到
			System.out.println("not find. del faild");	
			return;
		} else {
			System.out.println("list is empty del faild");			
		}	
	}
	

}
